<?php
namespace Modules\Home\Controllers;

class ServiceController extends ControllerBase {
  public function initialize() {
    parent::initialize();

    $this -> _checkLogin();
    $this -> view -> setLayout('index');
  }

  public function indexAction() {
    $this -> tag -> prependTitle('区域代表');

    $builder = $this -> modelsManager -> createBuilder()
      -> from([
        'us' => 'Common\Models\UserShopModel',
        'u' => 'Common\Models\UserModel'
      ])
      -> columns(
        'us.user_id, us.create_time, us.status, us.address_code, us.address_detail, us.real_name, '.
        'u.user_code, u.real_name as u_real_name'
      )
      -> where('us.user_id = u.user_id')
      -> orderBy('us.address_code ASC');

    $get = $this -> request -> getQuery();
    if(!empty($get['district_code'])) {
      $builder -> andWhere('address_code = "' . $get['district_code'] . '"');
    } else if(!empty($get['city_code'])) {
      $builder -> andWhere('address_code LIKE "' . substr($get['city_code'], 0, 4) . '%"');
    } else if(!empty($get['province_code'])) {
      $builder -> andWhere('address_code LIKE "' . substr($get['province_code'], 0, 2) . '%"');
    }

    $this -> view -> pager = $builder -> getQuery() -> execute();
    unset($get['_url']);
    $this -> view -> get = $get;
  }
}
?>